< previous page page_460 next page >

Page 460
9f31265bb40b592b1929e18a47ab28e5.gif
         Beep
         KeyAscii = 0      ' Cancels the keystroke
   End Select
End Sub
Of course, you would probably want to place this code in a generic function that you can call from any text box in which you want to trap for numeric input only.
Input Characters Only
To restrict a text box to the characters A to Z and a to z, use the following code in the KeyPress event of the text box:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub txtLetters_KeyPress(KeyAscii As Integer)
   Select Case KeyAscii
      Case vbKeyDelete     ' Delete Key
      Case vbKeyBack       ' Backspace Key
      Case 65 To 90        ' Upper Case Chars
      Case 97 To 122       ' Lower Case Chars
      Case Else
         Beep
         KeyAscii = 0      ' Cancels keystroke
   End Select
End Sub
As in the previous example, you should create a generic routine that you can call to perform the trap for these keystrokes.
Limiting Characters
To limit the number of input characters, set the number for the MaxLength property to the total number of characters the user can enter. In a data-bound text box, if you do not set this property and then allow the user to enter too many characters, an error might occur when the value back is placed in a table in the database.
Validating Data
Sometimes you need to get immediate feedback of invalid data when the user attempts to leave a field. To accomplish this, you can use the LostFocus() event in a text box.

 
< previous page page_460 next page >

If you like this book, buy it!